package com.erp.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.erp.user.domain.ErpBranchOfficeLimit;
import com.erp.user.domain.ErpDistributionUser;
import com.erp.user.domain.bo.ErpBranchOfficeLimitBo;
import com.erp.user.domain.vo.ErpBranchOfficeLimitVo;
import com.erp.user.mapper.ErpBranchOfficeLimitMapper;
import com.erp.user.service.IErpBranchOfficeLimitService;
import com.erp.user.service.IErpDistributionUserService;
import com.ruoyi.common.core.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.web.oldController.BaseController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * 分公司开户额度
 *
 * @author erp
 * @date 2023-05-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/distribution/limit")
public class ErpBranchOfficeLimitController extends BaseController {

    @Resource
    private IErpBranchOfficeLimitService branchOfficeLimitService;
    @Resource
    private ErpBranchOfficeLimitMapper limitMapper;
    @Resource
    private IErpDistributionUserService distributionUserService;

    /**
     * 根据userId查询名额
     */
    @GetMapping("/getLimit")
    public R<ErpBranchOfficeLimit> getLimit() {
        Long userId = SecurityUtils.getUserId();
        ErpBranchOfficeLimit branchOfficeInfo = new ErpBranchOfficeLimit();
        if (userId != 1) {
            ErpDistributionUser distributionUser = distributionUserService.getByUserId(userId);
            if (distributionUser == null){
                // 无记录，有问题
                return R.fail(branchOfficeInfo, "数据有误，请联系管理员。errorInfo=" + userId + "，该userId查询不到信息");
            }
            // 查询名额信息
            branchOfficeInfo = limitMapper.selectOne(new LambdaQueryWrapper<ErpBranchOfficeLimit>()
                    .eq(ErpBranchOfficeLimit::getBranchId, distributionUser.getDistributionUserId()));
            if (branchOfficeInfo == null){
                // 无记录
                branchOfficeInfo = new ErpBranchOfficeLimit();
                branchOfficeInfo.setBranchId(distributionUser.getDistributionUserId());
                branchOfficeInfo.setNotAgentNum(0L);
                branchOfficeInfo.setAgentNum(0L);
                branchOfficeInfo.setUserNum(0L);
                branchOfficeInfo.setNotUserNum(0L);
            }
        }else {
            // 超级管理员，放开数量限制
            branchOfficeInfo.setBranchId(1l);
            branchOfficeInfo.setNotAgentNum(100L);
            branchOfficeInfo.setAgentNum(100L);
            branchOfficeInfo.setUserNum(100L);
            branchOfficeInfo.setNotUserNum(100L);
        }
        return R.ok(branchOfficeInfo);
    }

    /**
     * 获取分公司开户额度详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<ErpBranchOfficeLimitVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(branchOfficeLimitService.queryById(id));
    }

    /**
     * 新增分公司开户额度
     */
    @Log(title = "分公司开户额度", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpBranchOfficeLimitBo bo) {
        return toAjax(branchOfficeLimitService.insertByBo(bo));
    }

    /**
     * 修改分公司开户额度
     */
    @Log(title = "分公司开户额度", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/edit")
    public R<Void> edit(@RequestBody ErpBranchOfficeLimitBo bo) {
        return toAjax(branchOfficeLimitService.updateByBo(bo));
    }

    /**
     * 删除分公司开户额度
     *
     * @param ids 主键串
     */
    @Log(title = "分公司开户额度", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(branchOfficeLimitService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
